package com.tanger.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.Arrays;

/**
 * mybatis config
 */
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = { "com.tanger"}, useDefaultFilters = true)
@MapperScan(value = {"com.tanger.*.mapper"})
public class DruidDataSourceConfig implements EnvironmentAware {

    private Environment environment;
    private DataSourcesProperties dataSourcesProperties;

    public void setEnvironment(Environment environment) {
        this.environment = environment;
        this.dataSourcesProperties = Binder.get(environment)
                .bind("spring.datasource", DataSourcesProperties.class)
                .orElse(null);
    }

    //注册dataSource
    @Bean(initMethod = "init", destroyMethod = "close")
    public DruidDataSource dataSource() throws SQLException {
        if (StringUtils.isEmpty(dataSourcesProperties.getUrl())) {
            System.out.println("Your database connection pool configuration is incorrect!"
                    + " Please check your Spring profile, current profiles are:"
                    + Arrays.toString(environment.getActiveProfiles()));
            throw new ApplicationContextException(
                    "Database connection pool is not configured correctly");
        }
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(dataSourcesProperties.getDriverClassName());
        druidDataSource.setUrl(dataSourcesProperties.getUrl());
        druidDataSource.setUsername(dataSourcesProperties.getUsername());
        druidDataSource.setPassword(dataSourcesProperties.getPassword());
//        druidDataSource.setInitialSize(dataSourcesProperties.getInitialSize());
//        druidDataSource.setMinIdle(dataSourcesProperties.getMinIdle());
//        druidDataSource.setMaxActive(dataSourcesProperties.getMaxActive());
//        druidDataSource.setMaxWait(dataSourcesProperties.getMaxWait());
//        druidDataSource.setTimeBetweenEvictionRunsMillis(dataSourcesProperties.getTimeBetweenEvictionRunsMillis());
//        druidDataSource.setMinEvictableIdleTimeMillis(dataSourcesProperties.getMinEvictableIdleTimeMillis());
//        druidDataSource.setValidationQuery(dataSourcesProperties.getValidationQuery());
//        druidDataSource.setTestWhileIdle(dataSourcesProperties.getTestWhileIdle());
//        druidDataSource.setTestOnBorrow(dataSourcesProperties.getTestOnBorrow());
//        druidDataSource.setTestOnReturn(dataSourcesProperties.getTestOnReturn());
//        druidDataSource.setPoolPreparedStatements(dataSourcesProperties.getPoolPreparedStatements());
//        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(dataSourcesProperties.getMaxPoolPreparedStatementPerConnectionSize());
        druidDataSource.setFilters(dataSourcesProperties.getFilters());
        return druidDataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws SQLException {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public PaginationInnerInterceptor paginationInterceptor() {
        return new PaginationInnerInterceptor();
    }
}
